#include "EWAUITrackHeaderAudio.hpp"

namespace Eyer
{
    EWAUITrackHeaderAudio::EWAUITrackHeaderAudio(EyerSmartPtr<EWAUITimelineContext> _timelineCtx, EWAUILayoutParams width, EWAUILayoutParams height)
        : EWAUITrackHeader(_timelineCtx, width, height)
    {
        SetWidgetName("EWAUITrackHeader_Audio");
        SetLayoutType(EWAUILayoutType::LinearLayoutVertical);
    }

    EWAUITrackHeaderAudio::~EWAUITrackHeaderAudio()
    {

    }

    int EWAUITrackHeaderAudio::OnTrackUpdate(EWAPrjTimeline * timeline)
    {
        // EyerLog("OnTrackUpdate\n");
        RemoveAllWidget();
        EWAPrjTrackType trackType = EWAPrjTrackType::AUDIO;
        for(int i = 0; i < timeline->GetTrackCount(trackType); i++) {
            EyerSmartPtr<EWAPrjTrackUI> track = timeline->GetTrack(i, trackType);

            AddWidget(track->item);
            AddWidget(track->segBar);
        }
        return 0;
    }

    bool EWAUITrackHeaderAudio::OnMouseMoveEvent      (const Vec2I & pos)
    {
        int lastY = 0;
        EWAPrjTrackType trackType = EWAPrjTrackType::AUDIO;
        for(int i = 0; i < timelineCtx->timeline.GetTrackCount(trackType); i++) {
            EyerSmartPtr<EWAPrjTrackUI> track = timelineCtx->timeline.GetTrack(i, trackType);
            EyerSmartPtr<EWAUITrackHeaderItem> item = track->item;
            EyerSmartPtr<EWAUIWidgetVerticalSegBar> segBar = track->segBar;

            if(segBar == nullptr){
                continue;
            }

            if(segBar->IsDraging()){
                // 处理拖拽
                int y = pos.y();

                // 计算高度
                int h = y - lastY;
                if(h <= 0){
                    continue;
                }
                item->SetMinHeight(h);
                continue;
            }

            lastY += item->wh.y();
            lastY += segBar->wh.y();
        }
        return true;
    }
}